import 'package:flutter/material.dart';
import '../widgets/platform/adaptive_card.dart';
import '../../core/platform/platform_detector.dart';

/// 快捷操作组件
class QuickActions extends StatelessWidget {
  final List<QuickAction> actions;
  final String? title;
  final VoidCallback? onCreateNew;

  const QuickActions({
    super.key,
    required this.actions,
    this.title,
    this.onCreateNew,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            if (title != null)
              SelectableText(
                title!,
                style: Theme.of(context).textTheme.titleLarge,
              ),
            if (onCreateNew != null)
              ElevatedButton(
                onPressed: onCreateNew,
                child: const SelectableText('创建新需求'),
              ),
          ],
        ),
        const SizedBox(height: 16),
        if (PlatformDetector.isDesktop() || PlatformDetector.isWeb())
          GridView.builder(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 5,
              crossAxisSpacing: 16,
              mainAxisSpacing: 16,
              childAspectRatio: 0.8,
            ),
            itemCount: actions.length,
            itemBuilder: (context, index) {
              final action = actions[index];
              return _buildQuickActionCard(context, action);
            },
          )
        else
          GridView.builder(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              crossAxisSpacing: 16,
              mainAxisSpacing: 16,
              childAspectRatio: 0.8,
            ),
            itemCount: actions.length,
            itemBuilder: (context, index) {
              final action = actions[index];
              return _buildQuickActionCard(context, action);
            },
          ),
      ],
    );
  }

  /// 构建快捷操作卡片
  Widget _buildQuickActionCard(BuildContext context, QuickAction action) {
    return AdaptiveCard(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SelectableText(
            action.title,
            style: const TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 14,
            ),
          ),
          const SizedBox(height: 8),
          // 进度条
          ClipRRect(
            borderRadius: BorderRadius.circular(4),
            child: LinearProgressIndicator(
              value: action.progress / 100,
              backgroundColor: Colors.grey[200],
              valueColor: AlwaysStoppedAnimation<Color>(
                _getProgressColor(action.progress),
              ),
            ),
          ),
          const SizedBox(height: 8),
          SelectableText(
            '负责人：${action.assignee}',
            style: const TextStyle(
              fontSize: 12,
              color: Colors.grey,
            ),
          ),
          SelectableText(
            '截止：${action.dueDate}',
            style: const TextStyle(
              fontSize: 12,
              color: Colors.grey,
            ),
          ),
        ],
      ),
    );
  }

  /// 根据进度获取颜色
  Color _getProgressColor(double progress) {
    if (progress < 30) return Colors.red;
    if (progress < 70) return Colors.orange;
    return Colors.green;
  }
}

/// 快捷操作数据模型
class QuickAction {
  final String title;
  final double progress;
  final String assignee;
  final String dueDate;

  QuickAction({
    required this.title,
    required this.progress,
    required this.assignee,
    required this.dueDate,
  });
}
